package com.itheima.ydd.controller;

import com.itheima.ydd.dto.AdvposPage;
import com.itheima.ydd.dto.SysUSerDo;
import com.itheima.ydd.entity.Result;
import com.itheima.ydd.entity.SysUser;
import com.itheima.ydd.service.SysUserService;
import com.itheima.ydd.utils.TextTransUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;


/**
 * @author Wang.XL
 * <p>
 * 管理员管理处理类
 */

@Slf4j
@RestController
@RequestMapping("sys_user")
public class SysUserController {
    @Autowired
    private SysUserService service;

    /**
     * 用户列表-分页
     *
     * @param loginName
     * @param status
     * @param phone
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("list")
    public Result list(String loginName, String status, String phone, String page, String limit) {
        AdvposPage pageInfo = service.findList(loginName, status, phone, page, limit);

        return new Result(0, "", pageInfo);
    }

    /**
     * 根据id删除
     *
     * @return
     */
    @GetMapping("del")
    public Result delSysUser(String id) {
        return service.delSysUser(id);
    }

    /**
     * 管理员添加
     *
     * @param sysUser
     * @return
     */
    @PostMapping("add")
    public Result addSysUser(@RequestBody Map<String, Object> sysUser) {
        return service.addSysUser(sysUser);
    }

    /**
     * 管理员更改
     *
     * @param sysUser
     * @return
     */
    @PostMapping("update")
    public Result updataSysUser(@RequestBody Map<String, Object> sysUser) {
        return service.updataSysUser(sysUser);
    }

    /**
     * 根据id进行查询
     *
     * @param id
     * @return
     */
    @GetMapping("rowInfo")
    public Result updateSysUserById(String id) {
        SysUser user = service.getById(id);
        //将密码置空
        //user.setPassword("");
        String roleId = user.getRoleId();
        List<String> list = TextTransUtil.doStringToArray(roleId);
        SysUSerDo sysUserDto = new SysUSerDo();
        BeanUtils.copyProperties(user,sysUserDto,"role_id");
        sysUserDto.setRole_id(list);
        return new Result(0, "", sysUserDto);
    }
}
